package codeTop;

public class q543 {
    public int diameterOfBinaryTree(TreeNode root) {
        dfs(root);
        return max-1;
    }
    int max = 0;
    int dfs(TreeNode node){
        if (node == null) {
            return 0;
        }
        int left = dfs(node.left);
        int right = dfs(node.right);
        max = Math.max(left+right+1,max);
        return Math.max(left,right)+1;
    }
}
